#include "cppTutorCopyConstructorVsAssignmentOperator.h"

#include <iostream>
#include <string.h>

using namespace std;

CcppTutorCopyConstructorVsAssignmentOperator :: CcppTutorCopyConstructorVsAssignmentOperator ()
{
}
CcppTutorCopyConstructorVsAssignmentOperator :: CcppTutorCopyConstructorVsAssignmentOperator (const char*cpValue = "")
{
	m_nValue = 0;
	m_nValue = strlen (cpValue) + 1;
	m_cpValue = new char[m_nValue];
	strncpy (m_cpValue, cpValue, m_nValue);
}

CcppTutorCopyConstructorVsAssignmentOperator :: ~CcppTutorCopyConstructorVsAssignmentOperator ()
{
	delete[] m_cpValue;
}

#ifdef USER_DEFINED_COPY_CONSTRUCTOR
CcppTutorCopyConstructorVsAssignmentOperator :: CcppTutorCopyConstructorVsAssignmentOperator (const CcppTutorCopyConstructorVsAssignmentOperator& tmp)
{
	this->m_nValue = tmp.m_nValue;
	if (tmp.m_cpValue != NULL)
	{
		m_cpValue = new char[this->m_nValue];
		strncpy (this->m_cpValue, tmp.m_cpValue, this->m_nValue);
	}
	else
	{
		this->m_cpValue = 0;
	}
}

#endif //USER_DEFINED_COPY_CONSTRUCTOR

#ifdef USER_DEFINED_ASSIGNMENT_OPERATOR
CcppTutorCopyConstructorVsAssignmentOperator& CcppTutorCopyConstructorVsAssignmentOperator :: operator= (const CcppTutorCopyConstructorVsAssignmentOperator &tmp)
{
	if (this == &tmp)
	{
		return *this;
	}
	//delete[] this->m_cpValue;

	this->m_nValue = tmp.m_nValue;

	if (tmp.m_cpValue != NULL)
	{
		this->m_cpValue = new char [this->m_nValue];
		strncpy (this->m_cpValue, tmp.m_cpValue, this->m_nValue);
	}
	else
	{
		m_cpValue = 0;
	}

	return *this;
}
#endif //USER_DEFINED_ASSIGNMENT_OPERATOR

void CcppTutorCopyConstructorVsAssignmentOperator :: m_GetValue ()
{
	cout << m_cpValue << endl;
}